﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QlrrPrintService.Print;
using QlrrPrintService.Common.Command;
using QlrrPrintService.Common;
using QlrrPrintService.Common.Extension;
using QlrrPrintService.Common.Sockets;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
           
        }




        AsyncSocketConnect<PluginRequestMessage> _socket;

        private PluginRequestMessage CreateMessage(PluginMessageType messageType,PrinterType priterType,
            object messageBody)
        {
            PluginRequestMessage message = new PluginRequestMessage();
            message.PrinterType = priterType;
            message.PluginMessageType = messageType;
            message.MessageBody = Newtonsoft.Json.JsonConvert.SerializeObject(messageBody);
            return message;
        }

        private void SendMessage(PluginRequestMessage message)
        {
            _socket = AsyncSocketConnect<PluginRequestMessage>.CreateClientSocket();
            _socket.ConnectToServer("127.0.0.1", 6000);
            _socket.IRecieveArgs = new AsyncReceiveArgs();
            _socket.OnReceivedMessage += new HandleMessageHandler<PluginRequestMessage>((args) =>
            {

            });
            _socket.AsyncSend(_socket.IRecieveArgs.CreateSendMsg(Newtonsoft.Json
                .JsonConvert.SerializeObject(message)));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PrintPaperTemplate template = new PrintPaperTemplate("test");
            template.Size = new QlrrPrintService.Common.Command.Size() { 
                M=95,
                N=100
            };
            template.Direction = new Direction() {
                M=0,
                N=0
            };
            template.Gap = new Gap() { 
                M=3,
                N=0
            };
            template.Bline = new Bline() {
                M=0,
                N=0
            };
            template.Offset = new Offset() {
                M=0,
            };
            template.Reference = new Reference() {
                X=0,
                Y=0
            };
            template.Feed = new Feed() { 
                N=0
            };
            template.BackFeed = new Backfeed() { 
                N=0
            };
            template.FormFeed = new FormFeed();
            template.Home = new Home();
            template.PriltLines = new List<Bar>();
            template.PriltLines.Add(new Bar() {
                X=35,
                Y=40,
                Width=90,
                Height=2
            });

            var image = Image.FromFile("Error1.bmp") as Bitmap;
            var base64 = image.ConvertTo1bppOr8bpp(1).BmpToByteArray();

            template.PrintBitmaps = new List<BitmapCommand>();
            template.PrintBitmaps.Add(new BitmapCommand() { 
                X=60,
                Y=60,
                Base64Code=Convert.ToBase64String(base64)
            });
            template.PrintQRCodes = new List<QRCode>();
            template.PrintQRCodes.Add(new QRCode() { 
                DataString="334www",
                X=80,
                Y=60
            });

            List<PrintPaper> papers = new List<PrintPaper>();
            PrintPaper paper = new PrintPaper();
            //paper.PrintPaperTemplate = template;
            paper.PrintFonts = new List<PrintFont>();
            paper.PrintFonts.Add(new PrintFont() { 
                X=0,
                Y=0,
                Content="这是测试测试",
                SzFaceName="黑体"
            });
            paper.PrintBarCodes = new List<BarCode>();
            paper.PrintBarCodes.Add(new BarCode()
            {
                X = 0,
                Y = 50,
                Code = "1234567"
            });
            papers.Add(paper);

            var template_json = Newtonsoft.Json.JsonConvert.SerializeObject(template);

            SendMessage(CreateMessage(PluginMessageType.Print,
                PrinterType.TSC, papers));
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            TSCPrinter printer = new TSCPrinter();
            PrintPaperTemplate template = new PrintPaperTemplate("test");
            template.Size = new QlrrPrintService.Common.Command.Size()
            {
                M = 95,
                N = 100
            };
            template.Direction = new Direction()
            {
                M = 0,
                N = 0
            };
            List<PrintPaper> papers = new List<PrintPaper>();
            PrintPaper paper = new PrintPaper();
            paper.PrintPaperTemplate = template;
            paper.PrintBarCodes = new List<BarCode>();
            paper.PrintBarCodes.Add(new BarCode() {
                X=0,
                Y=400,
                Code="1234567"
            });
            papers.Add(paper);
            printer.Print(new QlrrPrintService.Common.PluginRequestMessage()
            {
                MessageBody = Newtonsoft.Json.JsonConvert.SerializeObject(papers),
                PluginMessageType = PluginMessageType.Print,
                PrinterType = PrinterType.TSC
            });
            printer.ClosePrintert();
        }
    }
}
